package com.fk.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fk.dao.impl.BannerDaoImpl;
import com.fk.dao.impl.DesignerDaoImpl;
import com.fk.entity.Banner;
import com.fk.entity.Designer;
import com.fk.uitl.BaseServlet;
import com.fk.uitl.Result;
import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;


/**
 * Servlet implementation class AdminBannerListServler
 */
public class AdminBannerListServler extends BaseServlet {

	/**
	 * 查询轮播图
	 * localhost:8080/wuyijiuchaoliufushiguan/adminBannerList?reqName=getBannerList
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getBannerList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		BannerDaoImpl bDao = new BannerDaoImpl(); // 创建实体类对象
		List<Banner> banner = bDao.getBannerList(); // 调用方法

		response.setCharacterEncoding("UTF-8"); // 设置编码格式
		PrintWriter out = response.getWriter(); // out 对象

		Gson gson = new Gson();
		String res = gson.toJson(banner);
		out.print(res);

		out.flush();
		out.close();

	}

	/**
	 * 删除轮播图
	 * localhost:8080/wuyijiuchaoliufushiguan/adminBannerList?reqName=delGetBanner
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delGetBanner(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int bId = Integer.parseInt(request.getParameter("bId"));

		BannerDaoImpl bDao = new BannerDaoImpl(); // 创建实体类对象
		int b = bDao.delBanner(bId); // 调用删除方法

		response.setCharacterEncoding("UTF-8"); // 设置编码格式
		PrintWriter out = response.getWriter(); // 创建out对象

		if (b > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}

		out.flush();
		out.close();

	}

	/**
	 * ɾ���ֲ�ͼ
	 * localhost:8080/wuyijiuchaoliufushiguan/adminBannerList?reqName=addBanner
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addBanner(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		BannerDaoImpl baDao = new BannerDaoImpl();// 产品分类的是先类对象

		String bannerImg = request.getParameter("picName");
		if (bannerImg != null && !bannerImg.equals("")) {
			List<Banner> baList = baDao.getBannerByImg(bannerImg);
			if (baList.size() == 0) {
				Banner ba = new Banner(0, bannerImg);
				int b = baDao.addBanner(ba);
				if (b > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "照片已存在", ""));
			}
		}

		out.flush();
		out.close();
	}
	/**
	 * 图片上传
	 * localhost:8080/wuyijiu/adminBannerList?reqName=BannerUpload
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void BannerUpload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		
		//1.创建SmartUpload对象
		SmartUpload smart=new SmartUpload();
		//2.设置编码格式
		smart.setCharset("UTF-8");
		//3.初始化
		smart.initialize(getServletConfig(), request, response);
		
		
		try {//正常
			//4.上传
			smart.upload();
			//5.设置上传文件的名字
			String fileName=System.currentTimeMillis()+".jpg";
			//6.将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("/upload/"+fileName);
			fileName = "upload/"+fileName;
			//7.响应数据
			out.print(Result.toClient(true, "上传成功", fileName));
		} catch (SmartUploadException e) {//异常
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
	}
	
	/**
	 * 编辑轮播图
	 * localhost:8080/wuyijiu/adminBannerList?reqName=editBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void editBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		
		BannerDaoImpl bDao=new BannerDaoImpl();
		int bid=Integer.parseInt(request.getParameter("bannid"));
		Banner bann=bDao.getBannerById(bid);
		
		Gson gson=new Gson();
		String res=gson.toJson(bann);
		
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 修改轮播图
	 * localhost:8080/wuyijiu/adminBannerList?reqName=updateBanner
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateBanner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		
		BannerDaoImpl baDao=new BannerDaoImpl();
		
		int bannerId=Integer.parseInt(request.getParameter("bannerId"));
		String bannerImg=request.getParameter("bannerImg");
		
		Banner bann=new Banner(bannerId, bannerImg);
		int n=baDao.updatebanner(bann);
		if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		
		out.flush();
		out.close();
		
	}
}
